<?php


namespace app\service;


use think\Model;

abstract class BaseService
{
    protected $model;

    public abstract function __construct();

    protected function getModel(): Model
    {
        return $this->model;
    }

    /**
     * 根据ID获取一个值
     * @param $id
     * @param $field
     * @param $default
     * @return mixed
     */
    protected function getValueById($id, $field, $default = null)
    {
        $pk = $this->getPk();
        return $this->getModel()->where($pk, $id)->value($field, $default);
    }

    /**
     * 根据ID获取一行数据
     * @param $id
     * @param string $field
     * @return array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getOneLineById($id, $field = '*')
    {
        $pk = $this->getPk();
        return $this->getModel()->field($field)->where($pk, $id)->find();
    }

    /**
     * 根据ID获取一行数据
     * @param $id
     * @param string $field
     * @return Model
     * @throws \Exception
     */
    protected function getHiddenOneLineById($id, $field = '*')
    {
        try {
            $pk = $this->getPk();
            return $this->getModel()->withTrashed()->field($field)->where($pk, $id)->find();
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * 根据键值获取一行数据
     * @param $key
     * @param $val
     * @param string $field
     * @return Model
     * @throws \Exception
     */
    protected function getOneLineByKey($key, $val, $field = '*')
    {
        try {
            return $this->getModel()->field($field)->where($key, $val)->find();
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * 根据键值获取一行数据
     * @param $key
     * @param $val
     * @param string $field
     * @return Model
     * @throws \Exception
     */
    protected function getHiddenOneLineByKey($key, $val, $field = '*')
    {
        try {
            return $this->getModel()->withTrashed()->field($field)->where($key, $val)->find();
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * 根据条件获取一条数据
     * @param $where
     * @param string $field
     * @return Model
     * @throws \Exception
     */
    protected function getOneLineByWhere($where, $field = '*')
    {
        try {
            return $this->getModel()->field($field)->where($where)->find();
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * 根据条件获取所有数据
     * @param $where
     * @param string $field
     * @return array|\think\Collection|Model[]
     * @throws \Exception
     */
    protected function getListByWhere($where, $field = '*')
    {
        try {
            return $this->getModel()->field($field)->where($where)->select();
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * 模糊查询一列数据
     * @param $key
     * @param $val
     * @param string $field
     * @param string $fieldKey
     * @return array
     */
    protected function getColumnByLike($key, $val, $field = 'id', $fieldKey = '')
    {
        return $this->getModel()->where($key, 'like', '%' . $val . '%')->column($field, $fieldKey);
    }

    /**
     * 根据条件查询一列数据
     * @param $key
     * @param $val
     * @param string $field
     * @param string $fieldKey
     * @return array
     */
    protected function getColumnByField($key, $val, $field = 'id', $fieldKey = '')
    {
        return $this->getModel()->where($key, $val)->column($field, $fieldKey);
    }

    /**
     * 添加数据
     * @param $data
     * @return bool
     */
    protected function saveData($data)
    {
        return $this->getModel()->save($data);
    }

    /**
     * 获取主键
     * @return array|string
     */
    protected function getPk()
    {
        return $this->getModel()->getPk();
    }

    /**
     * 查询一条数据是否存在
     * @param $map
     * @param string $field
     * @return bool
     * @throws \think\db\exception\DbException
     */
    protected function exist($map, $field = '')
    {
        if (!is_array($map) && empty($field)) {
            $field = $this->getPk();
        }
        $map = !is_array($map) ? [$field => $map] : $map;
        return 0 < $this->getModel()->where($map)->count();
    }

    /**
     * 最后一条sql
     * @return string
     */
    protected function getLastSql()
    {
        return $this->getModel()->getLastSql();
    }
}